<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>文章中心--->修改</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="stylesheet" href="${ctxPath}/static/manage/css/x-admin.css" media="all">
    <script src="${ctxPath}/static/manage/js/jquery.js"></script>
    <script src="${ctxPath}/static/manage/layui/layui.js"></script>
    <script src="${ctxPath}/static/manage/js/x-layui.js" charset="utf-8"></script>
</head>
    <body>
        <form class="layui-form" id="formUpdate">
            <input type="hidden" name="id" value="${article.id}"/>
            <div class="layui-form-item">
                <label class="layui-form-label"><span class="x-red">*</span>文章类型</label>
                <div class="layui-input-inline typeSelect">
                    <select name="type" id="typeArticle" lay-filter="typeArticle">
                        <option value="">请选择</option>
                        <option value="1">文件公告</option>
                        <option value="2">行业动态</option>
                        <option value="3">政策法规</option>
                        <option value="0">其他文章</option>
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label"><span class="x-red">*</span>文章标题</label>
                <div class="layui-input-inline">
                    <input type="text" id="title" name="title" class="layui-input" value="${article.title}"/>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label"><span class="x-red">*</span>文章内容</label>
                <div class="layui-input-block">
                    <textarea class="layui-textarea" id="contentUpdate" name="content" style="display: none">${article.content}</textarea>
                </div>
            </div>
<!--            <input type="text" id="file" name="file" class="layui-input" value="${article.file}">-->
            <div class="layui-form-item">
                <label class="layui-form-label">附&nbsp;&nbsp;件：</label>
                <div class="layui-input-inline layui-btn" id="uploadArticleFile">
                    <i class="layui-icon">&#xe67c;</i>添加附件
                </div>
                <div class="layui-form-mid" id="showDocFiles"></div>
            </div>
            <button class="layui-btn" type="button" lay-filter="update" lay-submit="">保存修改</button>
        </form>
    </body>

    <script type="text/javascript">

        // 回显附件 TODO  ${ctxPath}        // 路径的前缀
        // var docFiles = $("#file").val();



        layui.use(['form', 'layedit', 'upload', 'layer'], function(){
            var $ = layui.$, form = layui.form, upload = layui.upload, layedit = layui.layedit;

            // 添加文章附件 上传服务器
            upload.render({
                elem: '#uploadArticleFile',
                size: 3000,
                exts: 'doc|docx|xls|xlsx|txt|pdf|zip|rar',
                url: '/kaptcha/docArticleUpload',
                done: function(result){
                    // layer.closeAll('loading');
                    // alert(result.data.src);
                    var docFile = result.data;

                    $("#showDocFiles").append('<a href='+docFile.src+' target="_blank">'+docFile.title+'</a>，');
                    var fileUrls = $("#file").val();
                    $("#file").val(fileUrls+","+docFile.src);

                },
                error: function(result){
                    layer.closeAll('loading');
                    alert(result.data);
                }
            });

            // 建立编辑器及图片保存初始化
            layedit.set({
                uploadImage: {
                    url: '/kaptcha/imageArticleUpload'
                }
            });
            var index = layedit.build('contentUpdate');

            // 监听提交
            form.on('submit(update)', function(){

                if($("#typeArticle").val()=="" || $("#typeArticle").val().length<0){
                    alert("文章类型没有选择");
                    return false;
                }
                if($("#title").val().trim()=="" || $("#title").val().trim().length<0){
                    alert("文章标题不能为空");
                    return false;
                }
                // 处理文章内容，<font face="仿宋">标签 双用时故障；
                var contentHtml = layedit.getContent(index);
                if(contentHtml=="" || contentHtml.length<0){
                    alert("文章内容不能为空");
                    return false;
                }
                contentHtml = contentHtml.replace(/<font face="仿宋">/g,"");
                $("#contentUpdate").val(contentHtml);

                $.ajax({
                    url:'/manage/article/articleUpdate',
                    type:'post',
                    data:$("#formUpdate").serialize(),
                    success:function(result){
                        alert(result);
                        window.parent.location.reload();
                    },
                    error:function(result){
                        alert("失败？请及时告知开发人员"+result);
                        window.parent.location.reload();
                    }
                });
            });

        });

    </script>
    <style>
        #uploadArticleFile{
        width: 140px;
    }
</style>
</html>